// PROGRAMA : SCRP130A
// SISTEMA : SISP - SISTEMA DE INFORMACOES DE SEGURANCA PUBLICA
// IDENTIFICACAO CRIMINAL
// BIBLIOTECA : SISP
// CLIENTE : GEJUSPC - GERENCIA DE ESTADO DA JUSTICA, SEGURANCA
// PUBLICA E CIDADANIA / INSTITUTO DE IDENT.
// OBJETIVO : EXCLUSAO DA FICHA DE INDICE CRIMINAL
// PROGRAMADOR: JOSE ANTONIO COELHO
// DATA : 29 DE SETEMBRO DE 1997
// ALTERACAO :
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrm1110;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrm2000;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp130aC;
import br.com.vti.sisp_sicrim.antigo.model.DidRg;
import br.com.vti.sisp_sicrim.antigo.model.Estla001;
import br.com.vti.sisp_sicrim.antigo.model.SicCadastro;
import br.com.vti.sisp_sicrim.antigo.model.SicNumero;
import br.com.vti.sisp_sicrim.antigo.model.SicSenha;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Scrg001;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Scrp130a OBJETIVO : EXCLUSAO DA FICHA DE INDICE CRIMINAL *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/scrp130a")
public class Scrp130a {
	// Views utilizadas
	SicSenha senhaView = new SicSenha();
	DidRg vwdidrg = new DidRg();
	SicCadastro criminalView = new SicCadastro();
	SicNumero numeroView = new SicNumero();
	// Telas/relatorios utilizados
	Scrm1110 scrm1110;
	Scrm2000 scrm2000;
	Scrp130aC scrp130aC;
	// Includes utilizados
	Estla001 estla001 = new Estla001();
	// Areas globais / session
	Scrg001 scrg001;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();
	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Scrp130a scrp130aAnterior;
	String faseproc = "0";

	private int jMatricula = 0;
	private String jDataNasc = "";
	private int jDataIni = 0;
	private int jDataFim = 0;
	private String jNomeDigitador = "";
	private int jDataDig = 0;
	private int jHorario = 0;
	private int var = 0;
	// VARIAVEIS DA ESTATISTICA
	private String mod = "";
	private int vlt = 0;
	private String[] tabm = { "", "   ", "VEI", "CON", "MUL", "   ", "IPV", "ROU", "   ", "   ", "   ", "   ", "   ", "IDC", "ICR", "FUN", "   ", "   " };
	private String mensagemInicial = "";

	private Scrm2000 preMapas() {
		sessao.getScrg001().setWProgramaw(this.getClass().getSimpleName().toUpperCase());
		sessao.getScrg001().setWFuncaow("Exclusao");
		sessao.getScrg001().setWVltw(16);

		scrm1110 = new Scrm1110(true, sessao);
		scrm2000 = new Scrm2000(true, sessao);
		scrp130aC = new Scrp130aC(true, sessao);

		scrm2000.setDataCorrente(UtilBean.obtemDataDia());
		scrm1110.setDataCorrente(UtilBean.obtemDataDia());

		return scrm2000;
	}

	public Scrp130a(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Scrm1110 scrm1110, Scrm2000 scrm2000, Scrp130aC scrp130aC) {
		try {
			main(scrm1110, scrm2000, scrp130aC);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroScrcerro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Scrm1110 pscrm1110, Scrm2000 pscrm2000, Scrp130aC pscrp130aC) throws Exception {
		if (pscrm1110 == null && pscrm2000 == null && pscrp130aC == null) {
			primeiraVez = true;
			// scrm2000 = new Scrm2000 (primeiraVez);
			scrm2000 = preMapas();
			scrm2000.setNavegar("../scrp130a/executar");
			scrm2000.setMensagem(mensagemInicial);
			result.include("scrm2000", scrm2000);
			sessao.setAtributo("SCRP130A", this);
			scrm2000.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(pscrm1110, pscrm2000, pscrp130aC);
		}

		switch (faseproc) {
		case "0": {
			scrm2000.receberTela(false, result);
			if (scrm2000.temMensagem) {
				result.include("scrm2000", scrm2000);
				sessao.setAtributo("SCRP130A", this);
				scrm2000.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
				Scrp1000 scrp1000 = new Scrp1000(result, sessao);
				scrp1000.executar(null);
			}

			// Par�grafo: fd33()
			while (criminalView.pesquisaRegEstadualGeral(scrm2000.getJregGeral())) {
				if (criminalView.getStatusReg() == 5) {
					scrm2000.enviarMensagem(null, "Ficha Criminal ja� foi Excluida anteriormente");
					result.include("scrm2000", scrm2000);
					sessao.setAtributo("SCRP130A", this);
					scrm2000.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
					return;
				}
				// Recupera os Dados
				scrm1110.setJregGeral(criminalView.getRegEstadualGeral());
				scrm1110.setJcpf(criminalView.getCpf());
				scrm1110.setJnomecompleto(criminalView.getNomeCompleto());
				if (criminalView.getPossuiMandatoAberto().trim().toUpperCase().equals("S")) {
					scrm1110.setJpmandp("SIM");
				} else {
					scrm1110.setJpmandp("NAO");
				}
				scrm1110.setJmae(criminalView.getNomeMae());
				jDataNasc = String.valueOf(criminalView.getDataNasc());
				scrm1110.setJdiaNasct(String.valueOf(UtilBean.subStr(jDataNasc, 6, 2)));
				scrm1110.setJmesNasct(String.valueOf(UtilBean.subStr(jDataNasc, 4, 2)));
				scrm1110.setJanoNasct(String.valueOf(UtilBean.subStr(jDataNasc, 0, 4)));
				scrm1110.setJpai(criminalView.getNomePai());
				scrm1110.setJalcunha(criminalView.getApelidoAlcunhas());
				scrm1110.setJnatural(criminalView.getNaturalidade());
				scrm1110.setJcodmunnascimento(criminalView.getCodigoMunicipioNasc());
				scrm1110.setJufnascimento(criminalView.getUfNaturalidade());
				scrm1110.setJocorrencia(1);
				scrm1110.setJobservacoes(criminalView.getObservacao());
				jDataDig = criminalView.getDataDocAtu();
				jMatricula = criminalView.getMatriculaFuncAtu();
				jHorario = criminalView.getHoraAtu();
				jDataIni = jDataDig;

				// Par�grafo: fd2()
				if (senhaView.consultaMatricula(sessao.getScrg001().getWMatriculaw())) {
					jNomeDigitador = senhaView.getNome();
					if (senhaView.getNivel() == 4) {
						scrm2000.setMensagem("                    USUARIO NAO AUTORIZADO PARA DELETAR");
						result.include("scrm2000", scrm2000);
						sessao.setAtributo("SCRP130A", this);
						scrm2000.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
						return;
					}
				} else {
					scrm2000.setMensagem("USUARIO NAO CADASTRADO");
					result.include("scrm2000", scrm2000);
					sessao.setAtributo("SCRP130A", this);
					scrm2000.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
					return;
				}

				faseproc = "1";
				result.include("scrm1110", scrm1110);
				sessao.setAtributo("SCRP130A", this);
				scrm1110.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (criminalView.registrosProcessados() == 0) {
				scrm2000.setMensagem("                   NENHUMA FICHA CRIMINAL FOI ENCONTRADA");
			}
			break;
		}

		case "1": {
			scrm1110.receberTela(false, result);

			faseproc = "2";
			result.include("scrp130aC", scrp130aC);
			sessao.setAtributo("SCRP130A", this);
			scrp130aC.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		case "2": {
			scrp130aC.receberTela(false, result);
			if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
				scrp130aC.enviarMensagem(null, "Tecla invalida");
				return;
			}
			if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
				if (sessao.getScrg001().getWMatriculaw() == 339838 || sessao.getScrg001().getWMatriculaw() == 34165) {
					Scrpdel scrpdel = new Scrpdel(result, sessao);
					scrpdel.executar(null);
					return;
				}
				Scrp1000 scrp1000 = new Scrp1000(result, sessao);
				scrp1000.executar(null);
				return;
			}
			if (!(scrp130aC.getJconf().toUpperCase().equals("S") || scrp130aC.getJconf().toUpperCase().equals("N"))) {
				scrp130aC.enviarMensagem("jconf", "Confirmacao invalida");
				result.include("scrp130aC", scrp130aC);
				sessao.setAtributo("SCRP130A", this);
				scrp130aC.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (scrp130aC.getJconf().toUpperCase().equals("N")) {
				// dao.rollback();
				if (sessao.getScrg001().getWMatriculaw() == 339838 || sessao.getScrg001().getWMatriculaw() == 34165) {
					Scrpdel scrpdel = new Scrpdel(result, sessao);
					scrpdel.executar(null);
					return;
				}
				Scrp130a scrp130a = new Scrp130a(result, sessao);
				scrp130a.executar(null, null, null);
				return;
			} else {
				criminalView.setStatusReg(5);
				criminalView.setTipoReg("EX");
				criminalView.atualiza();
				criminalView.fecha();
				// dao.commit();
				// Scrp130a scrp130a = new Scrp130a();
				// scrp130a.executar();
				// ATUALIZA DID-RG REGISTROS PARA ABN
				if (vwdidrg.consultaNumeroRg(scrm2000.getJregGeral())) {
					vwdidrg.exclui();
					// dao.commit();
					// dao.rollback();
				}
				vwdidrg.fecha();
				// ATUALIZA SIC-ESTATISTICAS NO CRIMINAL

				estla001.qde = 01;
				estla001.cpo = 04;
				// EXCLUS�O
				vlt = 14;
				var = 14;
				mod = tabm[14];
				estla001.esttip = 98;
				scrm2000.setMensagem("");

				funcoes.estna001(estla001.cpo, estla001.esttip, estla001.qde, var, sessao.getScrg001().getWMatriculaw());
			}
			break;
		}
		}

		faseproc = "0";
		sessao.setAtributo("SCRP130A", this);
		result.include("scrm2000", scrm2000);
		scrm2000.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
	}

	private void restaurarEstados(Scrm1110 pscrm1110, Scrm2000 pscrm2000, Scrp130aC pscrp130aC) {
		this.scrp130aAnterior = (Scrp130a) sessao.getAtributo("SCRP130A");
		if (pscrm1110 != null) {
			scrm1110 = pscrm1110;
			scrm1110.setJversaoj(sessao.getScrg001().getWVersaow());
			scrm1110.setJempresaj(sessao.getScrg001().getWEmpresaw());
			scrm1110.setJprogramaj(sessao.getScrg001().getWProgramaw());
		} else {
			scrm1110 = this.scrp130aAnterior.scrm1110;
		}
		scrm1110.setSessao(sessao);
		if (pscrm2000 != null) {
			scrm2000 = pscrm2000;
			scrm2000.setJversaoj(sessao.getScrg001().getWVersaow());
			scrm2000.setJempresaj(sessao.getScrg001().getWEmpresaw());
			scrm2000.setJprogramaj(sessao.getScrg001().getWProgramaw());
		} else {
			scrm2000 = this.scrp130aAnterior.scrm2000;
		}
		scrm2000.setSessao(sessao);
		if (pscrp130aC != null) {
			scrp130aC = pscrp130aC;
			scrp130aC.setjversaoj(sessao.getScrg001().getWVersaow());
			scrp130aC.setjempresaj(sessao.getScrg001().getWEmpresaw());
			scrp130aC.setjprogramaj(sessao.getScrg001().getWProgramaw());
		} else {
			scrp130aC = this.scrp130aAnterior.scrp130aC;
		}
		scrp130aC.setSessao(sessao);
		this.faseproc = this.scrp130aAnterior.faseproc;
		this.criminalView = this.scrp130aAnterior.criminalView;
	}

	protected void setMensagem(String mensagem) {
		mensagemInicial = mensagem;
	}
}
